#! /usr/bin/perl

use strict;

$ENV{MAUDE_LIB} = "/home/software/maude22";
my $maudepath = "/home/software/maude22/maude.linux";
my $pgmfile = "/home/software/javamop/plugins/PTCARET/ptCaRet.maude";

my $formula ;
my $res = "";

my $filename;
my $dot_input;
my $maude_input;

create_maude_input();
exec_maude();
print $res;
#create_output();
#print $html_output;
#print "\n\<dot\>\n";
#print $dot_input."\n";

sub create_maude_input {
    my $elt;
    my $ops = "";
    while (<>) {
	    if (/^[Ee]vent\s+([^\t <]+)[^:]*:/) {
	    	$elt = $1;
                $ops = $ops."  op $elt : -> Exp .\n";
	    }
		#hack so that we don't have to have :
		#but still can have : for input from the main
		#JavaMOP trial where we don't want to have to strip
		#off the pointcuts 
		elsif (/^[Ee]vent\s+([^\t <]+)/) {
	    	$elt = $1;
                $ops = $ops."  op $elt : -> Exp .\n";
	    }
	    elsif (/^[Pp]tcaret\s*:\s*(.*)/) {
		    $formula = $1;
	    }
    }    
    $formula =~ s/[\r\n]/ /g;
    my $f = $formula;
    $f =~ s/;//g;
		$f =~ s/\(\*\)/{*}/g;
		$f =~ s/\)s/}s/g;
    $f =~ s/\)w/}w/g;
	  $f =~ s/\!|\/\\|\\\/|\-\>|\<\-\>|\+\+|\(\*\)|\[\*\]|\<\*\>|\(\*a\)|\[\*a\]|\<\*a\>|\@b|\@c|\[\*s\@b\]|\<\*s\@b\>|\[\*s\@c\]|\<\*s\@c\>|\[\*s\@bc\]|\<\*s\@bc\>/ $& /g;
	  $f =~ s/\s+/ /g;

    $maude_input = "in $pgmfile\n";
    $maude_input = $maude_input."fmod TEST is pr PRETTY-PRINT-AND-OPTIMIZATION . \n";
    $maude_input = $maude_input.$ops;
    $maude_input = $maude_input."op formula : -> Formula . \n \n";
    $maude_input = $maude_input."eq formula = $f . \n";
    $maude_input = $maude_input."endfm \n \n";
    $maude_input = $maude_input."red genMonitor(formula) . \n\n quit .\n";
}

sub exec_maude{

    my $i = 0;
    while (-e "/tmp/ptltl.out$i") {
        $i++;
    }

    open MAUDEIN, "|$maudepath > /tmp/ptltl.out$i" || die "Cannot execute $maudepath\n";
    print MAUDEIN $maude_input;
    close MAUDEIN;

    open MAUDEOUT, "</tmp/ptltl.out$i" || die "Cannot open /tmp/ptltl.out$i\n";
    my $flag = 0;
    my $line;
    while($line = <MAUDEOUT>){
        chomp($line);
        if($line =~ /^result Code: (.*)$/){
            $flag = 1;
            $res = "<pre>\nmonitor contains:\n";
        }
        else {
            if($line =~ /^Bye/) {
                $flag = 0;
            }
            if($flag) {
            	  if ($line =~ /^\/\//) {
            	  	$line =~ s/\/\/\s+//;
            	  	if ($line =~ /^\d/) {
            	  		$line = "\t".$line;
            	  	}
            	  } elsif ($line =~ /output\((.*)\)/) {
            		   $line = "accepting condition:\n\t".$1;
            	  } else {
            	  	 $line = "\t".$line;
            	  }
                $res = $res.$line."\n";
            }
        }
    }
	$res .= "</pre>\n";
    $res .= "<INPUT TYPE='button' value='Output Syntax Help'" ;
    $res .=
          " onclick='javascript:window.open(\"http://fsl.cs.uiuc.edu/index.php/PTCaRet_Plugin_Output_Syntax\")'>" ;
 
    close(MAUDEOUT);
    system("rm -f /tmp/ptltl.out$i");
}


